package com.ucpaas.sms.service;

import com.jsmsframework.audit.entity.JsmsAuditCgroupRefClient;
import com.jsmsframework.audit.entity.JsmsAuditClientGroup;
import com.jsmsframework.audit.entity.JsmsAuditKeywordGroup;
import com.jsmsframework.audit.service.JsmsAuditCgroupRefClientService;
import com.jsmsframework.audit.service.JsmsAuditClientGroupService;
import com.jsmsframework.audit.service.JsmsAuditKeywordGroupService;
import com.jsmsframework.user.audit.service.JsmsUserAuditClientGroupService;
import com.ucpaas.sms.service.smsAudit.SmsAuditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by xiongfenglin on 2017/10/31.
 *
 * @author: xiongfenglin
 */
@Service
public class KeyWordGroupServiceImpl implements KeyWordGroupService {
    @Autowired
    private JsmsUserAuditClientGroupService jsmsUserAuditClientGroupService;
    @Autowired
    private JsmsAuditKeywordGroupService jsmsAuditKeywordGroupService;
    @Autowired
    private JsmsAuditClientGroupService jsmsAuditClientGroupService;
    @Autowired
    private SmsAuditService smsAuditService;
    @Autowired
    private JsmsAuditCgroupRefClientService jsmsAuditCgroupRefClientService;

    @Override
    @Transactional("message")
    public int modifykeywordGroupSave(JsmsAuditKeywordGroup model, List<String> categoryNameList) {


        int i= jsmsAuditKeywordGroupService.update(model,categoryNameList);
        if(i>0){
            List<String> clientIds = jsmsAuditKeywordGroupService.getRefClientIdsByGroupId(model.getKgroupId());
            smsAuditService.disableCache(clientIds);
        }
        return i;
    }

    @Override
    @Transactional("message")
    public int addkeywordGroupSave(JsmsAuditKeywordGroup model, List<String> categoryNameList) {
        int insert = jsmsAuditKeywordGroupService.insert(model, categoryNameList);
        if(insert>0){
            List<String> clientIds = jsmsAuditKeywordGroupService.getRefClientIdsByGroupId(model.getKgroupId());
            smsAuditService.disableCache(clientIds);
        }
        return insert;
    }

    @Override
    @Transactional("message")
    public int configurationkeyword(Map<String, String> params,JsmsAuditClientGroup jsmsAuditClientGroup) {
         List<String> clientIds = jsmsAuditKeywordGroupService.getRefClientIdsByGroupId(jsmsAuditClientGroup.getKgroupId());
        smsAuditService.disableCacheNoRefresh(clientIds);
        List<JsmsAuditCgroupRefClient> refClients = jsmsAuditCgroupRefClientService.getCgroupId(jsmsAuditClientGroup.getCgroupId());
        for(JsmsAuditCgroupRefClient ref:refClients){
            smsAuditService.doDisable(ref.getClientid());
        }

        int configurationkeyword = jsmsUserAuditClientGroupService.configurationkeyword(params, jsmsAuditClientGroup);
        for (int i = 0; i < params.size(); i++) {
            smsAuditService.doDisable(params.get("list[" + i + "][clientid]"));
        }
        return configurationkeyword;
    }

    @Override
    @Transactional("message")
    public int deteleJsmsAuditKeywordGroup(Integer kgroupId) {
        int i = jsmsAuditKeywordGroupService.deteleJsmsAuditKeywordGroup(kgroupId);
        if(i>0){
            List<String> clientIds = jsmsAuditKeywordGroupService.getRefClientIdsByGroupId(kgroupId);
            smsAuditService.disableCache(clientIds);
        }
        return i;
    }

    @Override
    @Transactional("message")
    public int deteleJsmsAuditClientGroup(Integer cgroupId) {
        int i = jsmsAuditClientGroupService.deteleJsmsAuditClientGroup(cgroupId);
        if(i>0){
            List<String> clientIds = jsmsAuditKeywordGroupService.getRefClientIdsByGroupId(cgroupId);
            smsAuditService.disableCache(clientIds);
        }
        return i;
    }
}
